﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.MathLib.CSG
{
   public class Cork
    {
         static bool checkSolid(RawMesh rawMesh)
        {
            var mesh = new Mesh(rawMesh);

            bool solid = true;

            if (mesh.isSelfIntersecting())
            {
                CORK_ERROR("isSolid() was given a self-intersecting mesh");
                solid = false;
            }

            if (!mesh.isClosed())
            {
                CORK_ERROR("isSolid() was given a non-closed mesh");
                solid = false;
            }

            return solid;
        }

        public static void computeUnion(RawMesh in0, RawMesh in1, out RawMesh outMesh)
        {
            var cmIn0 = new Mesh(in0); 
            var cmIn1 = new Mesh(in1); 
            cmIn0.boolUnion(cmIn1);
            outMesh = cmIn0.GetRaw();
        }

        // result = A - B
        public static void computeDifference(RawMesh in0, RawMesh in1, out RawMesh outMesh)
        {
            var cmIn0 = new Mesh(in0); 
            var cmIn1 = new Mesh(in1); 
            cmIn0.boolDiff(cmIn1);
            outMesh = cmIn0.GetRaw();
        }

        // result = A ^ B
        public static void computeIntersection(RawMesh in0, RawMesh in1, out RawMesh outMesh)
        {
            var cmIn0 = new Mesh(in0); 
            var cmIn1 = new Mesh(in1); 
            cmIn0.boolIsct(cmIn1);
            outMesh = cmIn0.GetRaw();
        }

        // result = A XOR B
        public static void computeSymmetricDifference(RawMesh in0, RawMesh in1, out RawMesh outMesh)
        {
            var cmIn0 = new Mesh(in0); 
            var cmIn1 = new Mesh(in1); 
            cmIn0.boolXor(cmIn1);
            outMesh = cmIn0.GetRaw();
        }

        // Not a Boolean operation, but related:
        //  No portion of either surface is deleted.  However, the
        //  curve of intersection between the two surfaces is made explicit,
        //  such that the two surfaces are now connected.
        public static void resolveIntersections(RawMesh in0, RawMesh in1, out RawMesh outMesh)
        {
            var cmIn0 = new Mesh(in0); ;
            var cmIn1 = new Mesh(in1); ;
            cmIn0.disjointUnion(cmIn1);
            cmIn0.resolveIntersections();
            outMesh = cmIn0.GetRaw();
        }

        public static void CORK_ERROR(string err)
        {
        }

    }
}
